package com.jmex.physics.impl.bullet.geometry;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import com.jme.math.Matrix3f;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.TriMesh;
import com.jmex.physics.PhysicsNode;
import com.jmex.physics.geometry.PhysicsMesh;

public class BulletMesh extends PhysicsMesh implements BulletGeometry {

	private float[] vertices;
	private int[] triangles;
	
    private boolean dirty;
    private com.jme.math.Vector3f previousScale = new com.jme.math.Vector3f();
    private Quaternion previousRotation = new Quaternion();
    private com.jme.math.Vector3f previousTranslation = new com.jme.math.Vector3f();
	
	private Vector3f centerOfMass = new Vector3f();
	private Matrix3f inertia = new Matrix3f();
	private float volume=1;
	
	public BulletMesh(PhysicsNode node) {
		super(node);
	}

	public float[] getVerticesArray(){return vertices;}
	public int[] getTrianglesArray(){return triangles;}
	
	@Override
	public void copyFrom(TriMesh triMesh) {
		//This is probably the LEAST efficient way to do this, but it'll work!
		Vector3f[] triangleVertices = new Vector3f[triMesh.getTriangleCount()*3];
		triMesh.getMeshAsTrianglesVertices(triangleVertices);
		vertices=new float[triangleVertices.length * 3];
		triangles=new int[triangleVertices.length];
		Vector3f temp = new Vector3f();
		for(int loop=0; loop<triangleVertices.length; loop++)
		{
			triangleVertices[loop].mult(getLocalScale(),temp);
			vertices[loop*3]=temp.x;
			vertices[loop*3+1]=temp.y;
			vertices[loop*3+2]=temp.z;
			triangles[loop]=loop;
		}
	}

	@Override
	public void copyFrom(TriMesh triMesh, float volume, Vector3f centerOfMass,
			Matrix3f inertia) {
		copyFrom(triMesh);
		this.volume=volume;
		this.centerOfMass.set(centerOfMass);
		this.inertia.copy(inertia);
	}

	@Override
	protected void drawDebugShape(PhysicsNode physicsNode, Renderer renderer) {
		// TODO Auto-generated method stub

	}

	@Override
	public float getVolume() {
		return volume;
	}

	public float[] getLocalRotationAsArray() {
		return new float[]{getLocalRotation().x,getLocalRotation().y,getLocalRotation().z,getLocalRotation().w};
	}

	public float[] getLocalScaleAsArray() {
		return new float[]{getLocalScale().x,getLocalScale().y,getLocalScale().z};
	}

	public float[] getLocalTranslationAsArray() {
		return new float[]{getLocalTranslation().x,getLocalTranslation().y,getLocalTranslation().z};
	}

	public Shape getShape() {
		return Shape.Mesh;
	}

	public int getShapeOrdinal() {
		return Shape.Mesh.ordinal();
	}

    public boolean isDirty() {
        if ( dirty ) {
            return true;
        }
        return !( getLocalTranslation().equals( previousTranslation ) &&
                getLocalRotation().equals( previousRotation ) &&
                getLocalScale().equals( previousScale ) );
    }

    public void setDirty( boolean value ) {
        dirty = value;
        if ( !value ) {
            previousTranslation.set( getLocalTranslation() );
            previousRotation.set( getLocalRotation() );
            previousScale.set( getLocalScale() );
        }
    }

}
